package com.gf.research.cockpit.controller;


import com.gf.research.cockpit.sirm.mapper.SirmTestMapper;
import com.gf.research.cockpit.utils.HealthUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 依赖监控接口，用于查询依赖项是否OK
 *
 * @Author: wangjunchao(王俊超)
 * @Time: 2018-08-07 13:08
 **/
//@Api("应用健康状态监控API")
@RestController()
@RequestMapping("/health")
public class HealthController {
    private static final Logger logger = LoggerFactory.getLogger(HealthController.class);

    /*@Resource
    @Qualifier("commonRedisTemplate")
    private RedisTemplate template;

    @Resource
    private PortalTestMapper portalTestMapper;

    @Resource
    private SirmTestMapper sirmTestMapper;

    @Resource
    private AmazonS3 amazonS3;

    @Resource
    private JavaMailSenderImpl mailSender;

    @Autowired
    private ConfigHelper configHelper;

    @Autowired
    private CommonConfigHelper commonConfigHelper;*/

    @Resource
    private SirmTestMapper sirmTestMapper;

/*
    @Limit(
            permit = 10,
            preheat = 100,
            permitTimeUnit = TimeUnit.SECONDS,
            preheatTimeUnit = TimeUnit.MILLISECONDS,
            scope = Limit.Scope.GLOBAL,
            rate = Limit.Rate.SMOOTH)
    @ApiOperation(value = "应用健康状态监控", notes = "")*/
    @RequestMapping(value = "", method = RequestMethod.GET)
    public Object health() {
        /*ServiceResponse response = ResponseUtils.getSuccessResponse(
                MessageFormat.format("{0}[{1}][{2}] health check",
                        SpringContextUtils.getApplicationName(),
                        SpringContextUtils.getVersion(), getInstanceId())
        );*/


        HealthUtils.Status currStatus = HealthUtils.testDbConnection("sirm", sirmTestMapper);
        boolean status = currStatus.isHealth();
        List<Object> dbTestList = new ArrayList<>();
        dbTestList.add(currStatus);

//        currStatus = HealthUtils.testDbConnection("portal", portalTestMapper);
//        status &= currStatus.isHealth();
//        dbTestList.add(currStatus);
//        response.put("db", dbTestList);


//        currStatus = HealthUtils.testMailConnection("portal", mailSender);
//        status &= currStatus.isHealth();
//        List<Object> mailTestList = new ArrayList<>();
//        mailTestList.add(currStatus);
//        response.put("mail", mailTestList);
//
//        currStatus = HealthUtils.testS3Connection("amazon", amazonS3);
//        status &= currStatus.isHealth();
//        List<Object> s3TestList = new ArrayList<>();
//        s3TestList.add(currStatus);
//        response.put("s3", s3TestList);
//
//        currStatus = HealthUtils.testRedisConnection("portal", template);
//        status &= currStatus.isHealth();
//        List<Object> redisTestList = new ArrayList<>();
//        redisTestList.add(currStatus);
//        response.put("redis", redisTestList);
//
//        // 小程序获取token的接口检查
//        currStatus = HealthUtils.testHttpConnection("access_token_url", commonConfigHelper.findByKey(CommonProps.WEIXIN_GET_ACCESS_TOKEN));
//        status &= currStatus.isHealth();
//        List<Object> weixinTestList = new ArrayList<>();
//        weixinTestList.add(currStatus);
//        response.put("weixin", weixinTestList);
//
//        //会见
//        currStatus = HealthUtils.testHttpConnection("meet", configHelper.findByKey(Props.MEETING_MEET_HOST));
//        status &= currStatus.isHealth();
//        List<Object> meetTestList = new ArrayList<>();
//        meetTestList.add(currStatus);
//        response.put("meet", meetTestList);
//
//        //进门财经
//        currStatus = HealthUtils.testHttpConnection("comeIn", configHelper.findByKey(Props.COMEIN_HOST));
//        status &= currStatus.isHealth();
//        List<Object> comeInTestList = new ArrayList<>();
//        comeInTestList.add(currStatus);
//        response.put("comeIn", comeInTestList);
//
//        // 广发通服务
//        currStatus = HealthUtils.testHttpConnection("gft", configHelper.findByKey(Props.GFT_SSO_HEALTH_URL));
//        status &= currStatus.isHealth();
//        List<Object> gftTestList = new ArrayList<>();
//        gftTestList.add(currStatus);
//        response.put("gft", gftTestList);
//
//        response.put("status", status);
        return dbTestList;
    }

    private String getInstanceId() {
        try {
            InetAddress localhost = InetAddress.getLocalHost();
            return localhost.getHostName();
        } catch (Exception var2) {
            return UUID.randomUUID().toString();
        }
    }


}
